import java.util.HashSet;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

public class RemoveDuplicatesfromSortedList {
	public class ListNode {
		 int val;
		 ListNode next;
		 ListNode(int x) { val = x; }
	}
//    public ListNode deleteDuplicates(ListNode head) {
//        HashSet<Integer> set=new HashSet<>();
//        ListNode curNode=head;
//        ListNode lastNode=new ListNode(0);
//        lastNode.next=head;
//        while(curNode!=null){
//            if(!set.add(curNode.val)){//删除当前结点
//                lastNode.next=curNode.next;//last不变
//                curNode=curNode.next;
//            }else{
//                //遍历下一个节点,更新last
//                lastNode=curNode;
//                curNode=curNode.next;
//            }
//        }
//        return head;
//    }
    public ListNode deleteDuplicates(ListNode head) {
        ListNode curNode=head;
        ListNode lastNode=new ListNode(head.val-1);
        lastNode.next=head;
        while(curNode!=null){
        	if(lastNode.val==curNode.val){
                lastNode.next=curNode.next;//last不变
            }else{
                //遍历下一个节点,更新last
                lastNode=curNode;
            }
        	curNode=curNode.next;
        }
        return head;
    }
    public static void main(String[] args) {
    	RemoveDuplicatesfromSortedList removeDuplicatesfromSortedList=new RemoveDuplicatesfromSortedList();
		ListNode node1=removeDuplicatesfromSortedList.new ListNode(1);
		ListNode node2=removeDuplicatesfromSortedList.new ListNode(1);
		node1.next=node2;
		ListNode curNode=removeDuplicatesfromSortedList.deleteDuplicates(node1);
		while (curNode!=null) {
			System.out.println(curNode.val);
			curNode=curNode.next;
		}
	}
}
